package com.sugon.controller.tool;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.sugon.annotation.AutoLog;
import com.sugon.annotation.NonBusinessParamValid;
import com.sugon.entity.ToolsCheckPlanEntity;
import com.sugon.entity.ToolsCheckPlanItemEntity;
import com.sugon.modules.base.model.vo.ProcessProgressConst;
import com.sugon.modules.tools.model.vo.ToolsCheckPlanItemParamVO;
import com.sugon.modules.utils.DateUtil;
import com.sugon.service.ToolsCheckPlanItemService;
import com.sugon.service.ToolsCheckPlanService;
import com.sugon.utils.PageUtils;
import com.sugon.utils.Query;
import com.sugon.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 工装周期性检测计划明细表，设备关联Controller
 *
 * @author sugon
 * @date 2021-09-23 16:26:40
 */
@Controller
@RequestMapping("toolscheckplanitem")
public class ToolsCheckPlanItemController {

    @Autowired
    private ToolsCheckPlanItemService toolsCheckPlanItemService;
    @Autowired
    private ToolsCheckPlanService toolsCheckPlanService;

    /**
     * 查看列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("toolscheckplanitem:list")
    @ResponseBody
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);

        List<ToolsCheckPlanItemEntity> toolsCheckPlanItemList = toolsCheckPlanItemService.queryList(query);
        int total = toolsCheckPlanItemService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(toolsCheckPlanItemList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{devId}")
    //@RequiresPermissions("toolscheckplanitem:info")
    @ResponseBody
    public R info(@PathVariable("devId") String devId) {
        ToolsCheckPlanItemEntity toolsCheckPlanItem = toolsCheckPlanItemService.queryObject(devId);

        return R.ok().put("toolsCheckPlanItem", toolsCheckPlanItem);
    }

    /**
     * 保存
     */
    @NonBusinessParamValid
    @RequestMapping("/save")
    //@RequiresPermissions("toolscheckplanitem:save")
    @ResponseBody
    public R save(@Validated @RequestBody ToolsCheckPlanItemParamVO planItemParam, BindingResult bindingResult) {
        // 校验工装校验计划是否存在
        String planId = planItemParam.getPlanId();
        ToolsCheckPlanEntity checkPlan = toolsCheckPlanService.queryObject(planId);
        if (checkPlan == null) {
            return R.error("非法工装校验计划ID!");
        }
        // 校验设备状态
        Integer status = checkPlan.getStatus();
        if (!ProcessProgressConst.NEW.equals(status) && !ProcessProgressConst.PASSED.equals(status)) {
            return R.error("只有处于编辑中或审核遭拒的计划才可以添加设备!");
        }
        ToolsCheckPlanItemEntity toolsCheckPlanItem;
        List<String> devIdList = planItemParam.getDevIdList();

        // 获取检测计划中支持的月份列表
        List<String> planMonthRangeList = DateUtil.getNormMonthRangeList(checkPlan.getStartDate(), checkPlan.getEndDate());
        for (String devId : devIdList) {
            toolsCheckPlanItem = new ToolsCheckPlanItemEntity();
            toolsCheckPlanItem.setPlanId(planId);
            toolsCheckPlanItem.setDevId(devId);
            // 设置个默认月份
            toolsCheckPlanItem.setPlanMonth(CollectionUtil.join(planMonthRangeList,":"));
            toolsCheckPlanItemService.save(toolsCheckPlanItem);
        }
        return R.ok();
    }

    @RequestMapping("/update")
    //@RequiresPermissions("toolscheckplanitem:update")
    @ResponseBody
    public R update(ToolsCheckPlanItemEntity toolsCheckPlanItem) {
        // 校验周期性检测计划明细ID 是否合法
        ToolsCheckPlanItemEntity CheckPlanItem = toolsCheckPlanItemService.queryObject(toolsCheckPlanItem.getId());
        Assert.notNull(CheckPlanItem, "非法周期性检测计划明细ID！");
        ToolsCheckPlanEntity checkPlan = toolsCheckPlanService.queryObject(CheckPlanItem.getPlanId());
        Assert.notNull(checkPlan);
        // 明细的计划实施月份需要在计划实施之间
        String planMonth = toolsCheckPlanItem.getPlanMonth();
        if (StrUtil.isNotEmpty(planMonth)) {
            // 获取检测计划中支持的月份列表
            List<String> monthRangeList = DateUtil.getNormMonthRangeList(checkPlan.getStartDate(), checkPlan.getEndDate());
            String[] devPlanMonthArray = planMonth.split(":");
            for (String devPlanMonth : devPlanMonthArray) {
                Assert.isTrue(monthRangeList.contains(devPlanMonth), "设备的实施月份需要在计划的起止月份{}之间！",monthRangeList);
            }
        }
        toolsCheckPlanItemService.update(toolsCheckPlanItem);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("toolscheckplanitem:delete")
    @ResponseBody
    public R delete(@RequestBody String[] devIds) {
        toolsCheckPlanItemService.deleteBatch(devIds);

        return R.ok();
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    @ResponseBody
    public R queryAll(@RequestParam Map<String, Object> params) {

        List<ToolsCheckPlanItemEntity> list = toolsCheckPlanItemService.queryList(params);

        return R.ok().put("list", list);
    }
}
